<?php

/**
 * @name SmsController
 * @desc 控制器
 */
class SmsController extends AdminCommonController
{
    /**
     * 默认动作
     */
    public function indexAction()
    {
        echo "Hello sms";
        return false;
    }

    //发送列表
    public function listAction(){
       $params = $this->jsonData();

       $row = $params['pageSize'] ?? 12;
       $page = $params['page'] ?? 1;

       $smsModel = SmsLogModel::getSingleton();

       $cond = [];
       if(isset($params['moble']) && $params['moble'] != ''){
           $cond['u.moble'] = trim($params['moble']);
       }
        if(isset($params['type']) && $params['type']){
            $cond['type'] = $params['type'];
        }
       if(isset($params['date_s']) && $params['date_s'] != ''){
           $cond['create_time#>='] = $params['date_s'];
       }
       if(isset($params['date_e']) && $params['date_e'] != ''){
           $cond['create_time#<'] = $params['date_e'];
       }

       $list = $smsModel->db
           ->where($cond)
           ->order('id desc')
           ->limit(($page - 1) * $row, $row)
           ->select();
       $total = $smsModel->db
           ->where($cond)
           ->count();

        $domain = $_SERVER['SERVER_NAME'];
       foreach($list as &$val){
           $val['type_show'] = SmsLogModel::$typeArr[$val['type']];
           if($domain != 'gsh.htota.com'){
               $val['code'] = substr($val['code'], 0, 1) . '**' . substr($val['code'], -1);
               $val['content'] = substr($val['content'], 0, 1) . '**' . substr($val['content'], -1);
           }
       }

       $data = [
           'list' => $list,
           'total' => intval($total),
           'current'   => intval($page),
           'pageSize'  => intval($row),
           'page'  => intval($page)
       ];

       $this->ajaxReturn(0, '', $data);
    }

    //配置列表
    public function configAction(){

        $params = $this->jsonData();

        $row = $params['pageSize'] ?? 12;
        $page = $params['page'] ?? 1;

        $sysSmsModel = SmsConfigModel::getSingleton();
        $list = $sysSmsModel->db
            ->where(['is_show'=> 1])
            ->order('id desc')
            ->limit(($page - 1) * $row, $row)
            ->select();
        $total = $sysSmsModel->db
            ->where([])
            ->count();

        $data = [
            'list' => $list,
            'total' => intval($total),
            'current'   => intval($page),
            'pageSize'  => intval($row),
            'page'  => intval($page)
        ];

        $this->ajaxReturn(0, '', $data);
    }

    //修改配置
    public function editAction(){
        $params = $this->jsonData();

        if(!$params || empty($params) || !isset($params['id'])){
            $this->ajaxReturn(400, '修改失败');
        }

        $sysSmsModel = SmsConfigModel::getSingleton();
        $res = $sysSmsModel->update(['id'=> $params['id']],
            ['username'=> $params['username'], 'password'=> $params['password'], 'update_time'=> time()]
        );

        if(!$res){
            $this->ajaxReturn(400, '修改失败');
        }
        $this->ajaxReturn(0, '修改成功');
    }
}
